import { defineStore } from 'pinia'
import { getUserCollections, addCollection, cancelCollection, checkUserCollected, getActivityCollectionCount } from '@/api/collection'

export const useCollectionStore = defineStore('collection', {
  state: () => ({
    collections: [],
    total: 0,
    loading: false,
    isCollected: false,
    collectionCount: 0
  }),
  
  actions: {
    // 获取用户收藏列表
    async getUserCollections(params) {
      this.loading = true
      try {
        const response = await getUserCollections(params)
        const { data } = response
        this.collections = data.records || []
        this.total = data.total || 0
        this.loading = false
        return Promise.resolve(data)
      } catch (error) {
        this.loading = false
        return Promise.reject(error)
      }
    },
    
    // 添加收藏
    async addCollection(activityId) {
      try {
        const response = await addCollection(activityId)
        this.isCollected = true
        return Promise.resolve(response)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 取消收藏
    async cancelCollection(activityId) {
      try {
        const response = await cancelCollection(activityId)
        this.isCollected = false
        return Promise.resolve(response)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 检查用户是否已收藏活动
    async checkUserCollected(activityId) {
      try {
        const response = await checkUserCollected(activityId)
        const { data } = response
        this.isCollected = data
        return Promise.resolve(data)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 获取活动收藏数量
    async getActivityCollectionCount(activityId) {
      try {
        const response = await getActivityCollectionCount(activityId)
        const { data } = response
        this.collectionCount = data
        return Promise.resolve(data)
      } catch (error) {
        return Promise.reject(error)
      }
    }
  }
}) 